﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace OCPL
{
    public class SkypeStatusControl : BaseFieldControl
    {
        private TextBox _textBox1;
        private string _value;
        private Literal _literal;
        private string _skypeIconStyle;
        private string _behaviour;

        public SkypeStatusControl(string skypeIconStyle, string behaviour)
        {
            this._skypeIconStyle = skypeIconStyle;
            this._behaviour = behaviour;
            this._textBox1 = new TextBox();
            this._textBox1.Width = 250;
            this._textBox1.Attributes.Add("class", "ms-long");
            this._literal = new Literal();
            this._literal.Text = "placeholder text";
        }

        public override object Value
        {
            get
            {
                return this._value;
            }
            set
            {
                this._value = value.ToString();
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            this.InitializeControlsLayout();
            this.InitializeControlsValues();

            if (this.ControlMode == SPControlMode.Display)
            {
                this.Controls.Add(this._literal);
            }
            else
            {
                this.Controls.Add(this._textBox1);
            }

        }

        protected override void Render(HtmlTextWriter output)
        {
            // the table breaks the rendering on DisplayForm, so only the label is rendered
            if (this.ControlMode == SPControlMode.Display)
            {
                this._literal.RenderControl(output);
            }
            else
            {
                this._textBox1.RenderControl(output);
            }
            
            // show error message (if there is one)
            if (!string.IsNullOrEmpty(this.ErrorMessage))
            {
                this.RenderValidationMessage(output);
            }
        }

        public override void UpdateFieldValueInItem()
        {
            this.EnsureChildControls();

            if (this._textBox1.Text.Length > 0)
            {
                this.Value = this._textBox1.Text;
            }
            else
            {
                this.Value = "";
            }
            this.ItemFieldValue = this.Value;
        }

        public override void Validate()
        {
            if (ControlMode == SPControlMode.Display || !this.IsValid)
            {
                return;
            }

            base.Validate();

            if (this.Field.Required && string.IsNullOrEmpty(this.Value.ToString()))
            {
                this.ErrorMessage = "Please provide the skype account name";
                this.IsValid = false;
                return;
            }

        }


        private void InitializeControlsLayout()
        {


            this.Controls.Add(this._textBox1);
            this.Controls.Add(this._literal);
        }

        private void InitializeControlsValues()
        {
            string currentValue = (string)this.ItemFieldValue;

            
            if (!string.IsNullOrEmpty(currentValue))
            {
                if (this.ControlMode == SPControlMode.Edit)
                {
                    this._textBox1.Text = currentValue;
                }
                else if (this.ControlMode == SPControlMode.Display)
                {
                    string behPart = _behaviour.ToLower().Replace(" ", "");
                    string urlPart = _skypeIconStyle.ToLower().Replace(" ", "");
                    this._literal.Text = "<a href=\"skype:" + currentValue + "?"+ behPart +"\"><img src=\"http://mystatus.skype.com/" + urlPart + "/" + currentValue + "\" border=\"0\"></a>";
                }
            }
        }
    }
}